import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';

// 本地开发启动https
import mkcert from "vite-plugin-mkcert";

// @ts-ignore
import { visualizer } from 'rollup-plugin-visualizer'

// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  build: {
    outDir: 'magic-new-tab-dist',
    rollupOptions: {
      output: {
        // manualChunks 配置
        manualChunks: {
          // 将 React 相关库打包成单独的 chunk 中 
          'react-vendor': ['react', 'react-dom'],
          // 将 Lodash 库的代码单独打包 
          'lodash': ['lodash'],
          // 将组件库的代码打包
          'jsqr': ['jsqr'],
          'library': ['moment'],
          'ui': ['magic-ui-react'],
        },
      },
    }
  },
  resolve: {
    alias: [{ find: '@', replacement: '/src' }],
  },
  server: {
    https: true,
    proxy: {
      '/api': {
        // target: 'http://localhost:3000',
        target: 'http://124.221.183.172:3000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/uploads': {
        // target: 'http://localhost:3000',
        target: 'http://124.221.183.172:3000',
        changeOrigin: true,
      },
    }
  },
  plugins: [
    react(),
    mkcert(),
    visualizer(),
  ],
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
        additionalData: `@import "${resolve(__dirname, 'src/style/globalClass/index.less')}";`
      },
    }
  },
});
